package com.manor.paradise.activity

import android.view.View
import com.gyf.immersionbar.ktx.immersionBar
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.adapter.LybFeedHistoryAdapter
import com.manor.paradise.bean.*
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.lyb_activity_feed_history.*

class LybFeedHistoryActivity : AbsActivity() {
    private var pageNum = 1
    private val mList = arrayListOf<EmploymentBean.RecordsBean>()
    private val mAdapter by lazy {
        LybFeedHistoryAdapter(mList)
    }
    override fun getLayoutId() = R.layout.lyb_activity_feed_history
    override fun main() {
        super.main()
        immersionBar {
            statusBarColor(R.color.transparent)
            titleBarMarginTop(mTitleSpace)
            statusBarDarkFont(false)
        }
        mFeedHistoryRv.adapter = mAdapter
        setClick(mCloseIv)
        mFeedHistorySrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
        setData()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        if (v?.id == R.id.mCloseIv){
            finish()
        }
    }
    fun setData() {
        ElvesHttpUtil.getMyhirelist(pageNum, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var shopListBean: EmploymentBean = GsonUtil.gsonToBean<EmploymentBean>(info, EmploymentBean::class.java)
                KLog.e("json", shopListBean)
                if(pageNum==1){
                    mAdapter.setNewInstance(shopListBean.records)
                    mFeedHistorySrl?.finishRefresh()
                    if(shopListBean.records.size==0){
                        mEmptyView?.showNull()
                    }else{
                        mEmptyView?.visibility = View.GONE
                    }
                }else{
                    mAdapter.addData(shopListBean.records)
                    mFeedHistorySrl?.finishLoadMore()
                }

            }

        })
    }
    override fun onDestroy() {
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_HIRE_LIST)
        super.onDestroy()
    }
}